package com.loveyukari.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.loveyukari.dao.YukariMusciDao;
import com.loveyukari.model.Music;
import com.loveyukari.model.SongSheet;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
*
*yangshaohua
*2017年3月3日下午3:03:00
**/
@Service
public class MusicService {
	
	@Autowired
	private YukariMusciDao yukariMusciDao;
	@Autowired
	private RedisService redisService;
	

	private static final String MUSIC_RANDOM_TMP = "music:random:tmp";
	//临时随机列表
	public List<Music> getTmpRandomMusicList(){
		List<Music> list = redisService.rangList(MUSIC_RANDOM_TMP, Music.class);
		if (list == null){
			list = new ArrayList<Music>();
		}
		return list;
	}
	
	public JSONArray getAllMusicName(){
		List<Music> list = yukariMusciDao.selectListByAll();
		Collections.shuffle(list);
		JSONArray result = new JSONArray();
		for (Music music : list) {
			result.add(music.getName());
		}
		return result;
	}
	
	public List<Music> createTmpRandomMusicList(int size){
		List<Music> list = yukariMusciDao.selectListByAll();
		Random random = new Random();
		Set<Integer> randomSet = new HashSet<Integer>();
		List<Music> result = new ArrayList<Music>();
		while (true){
			int index = random.nextInt(list.size());
			boolean flag = randomSet.add(index);
			if (flag) {
				result.add(list.get(index));
			}
			if (result.size() == size){
				break;
			}
		}
		redisService.setListAll(MUSIC_RANDOM_TMP,result);
		return result;
	}
	
	public void clearTmpRandomMusicList(){
		redisService.del(MUSIC_RANDOM_TMP);
	}
	
	
	

	//获取列表
	public List<Music> searchMusicList(String type,String query,String orderType,boolean desc){
		List<Music> list = new ArrayList<Music>();
		String order = orderType;
		if (desc){
			order +=" desc";
		}
		list = yukariMusciDao.selectListBySearch(type, query, order);
		return list;
	}
	
	public Music getMusic(int id){
		Music music = yukariMusciDao.select(id);
		return music;
	}
	
	public void addMusic(Music music){
		if (StringUtils.isBlank(music.getName())){
			return;
		}
		if (StringUtils.isBlank(music.getType())){
			return;
		}
		yukariMusciDao.insertMusic(music);
	}
	
	public void editMusic(Music music){
		Integer id = music.getId();
		if (id == null){
			return;
		}
		Music oldMusic = yukariMusciDao.select(id);
		if (oldMusic == null){
			return;
		}
		if (music.getName() != null){
			oldMusic.setName(music.getName());
		}
		if (music.getNameCn() != null){
			oldMusic.setNameCn(music.getNameCn());
		}
		if (music.getDescription() != null){
			oldMusic.setDescription(music.getDescription());
		}
		if (music.getType() != null){
			oldMusic.setType(music.getType());
		}
		if (music.getContent() != null){
			oldMusic.setContent(music.getContent());
		}
		oldMusic.setStatus(music.getStatus());
		yukariMusciDao.updateMusic(oldMusic);
	}
	
	//直播歌单
	public List<JSONObject> getLiveSongCountList(){
		List<JSONObject> list = yukariMusciDao.selectSongSheetGroupDate();
		return list;
	}
	
	public List<Music> getLiveMusic(String date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date singDate = null;
		try {
			singDate = sdf.parse(date);
		} catch (ParseException e) {
		}
		List<Music> result = new ArrayList<Music>();
		if (date == null){
			return result;
		}
		List<Integer> list = yukariMusciDao.selectMusicIdbySheet(singDate);
		for (Integer musicId : list) {
			Music music = yukariMusciDao.select(musicId);
			if (music != null){
				result.add(music);
			}
		}
		return result;
	}
	
	
	public List<Music> searchMusicList(String query, int size){
		List<Music> list = yukariMusciDao.selectListBySize(query, size);
		return list;
	}
	
	public void addMusicSheet(int[] ids,int[] ranks,String date){
		if (ranks.length != ids.length){
			return;
		}
		Date singDate = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			singDate = sdf.parse(date);
		} catch (ParseException e) {
			return;
		}
		for (int i = 0; i < ranks.length; i++) {
			int musicId = ids[i];
			Music music = this.yukariMusciDao.select(musicId);
			if (music == null){
				continue;
			}
			SongSheet songSheet = new SongSheet();
			songSheet.setSingDate(singDate);
			songSheet.setRank(ranks[i]);
			songSheet.setMusicId(musicId);
			this.yukariMusciDao.insertSongSheet(songSheet);
			music.setLastSingTime(singDate);
			this.yukariMusciDao.updateMusic(music);
		}
		
	}
}
